package com.ronri.ams;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.CustomDialog;
import com.ronri.ams.bean.versionbean;
import com.ronri.common.AutoUpGrade;
import com.ronri.common.UrlEncode;
import com.ronri.db.RemoteDataService;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class activity_version extends AutoLayoutActivity {

    @BindView(R.id.btnBack)
    RelativeLayout btnBack;
    @BindView(R.id.title_text_tv)
    TextView titleTextTv;
    @BindView(R.id.tvVersion)
    RelativeLayout tvVersion;
    @BindView(R.id.fCurrentVersion)
    TextView fCurrentVersion;
    @BindView(R.id.fVersionInfo)
    TextView fVersionInfo;

    private Context mContext = null;
    private String szUrl = "";
    private Unbinder mUnbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version);
        mContext = this;
        mUnbinder=ButterKnife.bind(this);

        fCurrentVersion.setText("当前版本：V" + InitApplication.getVersionCode(this));
        fVersionInfo.setText(InitApplication.getVersionInfo(this));

        // 退出
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RemoteDataService rVersion = new RemoteDataService();
                szUrl = InitApplication.SystemConfig.getBase().RemoteAddress(
                        InitApplication.SystemConfig.getModule().getVersion().getAddress());
                Map<String, Object> mParam = new HashMap<>();

                mParam.put("addFrom", 1);
                mParam.put("addMark", "android");
                mParam.put("version", InitApplication.SystemConfig.getAppConfig().getVersion());

                rVersion.getRemoteData(szUrl, UrlEncode.getUrlParamsByMap(mParam, false),
                        InitApplication.SystemConfig.getModule().getLogin().getMethod(), new RemoteDataService.OnRemoteDataEvent() {
                            @Override
                            public void OnResultEvent(String szString) {
                                if (szString != null) {
                                    versionbean v = JSON.parseObject(szString, versionbean.class);//  JSON.parseObject(szString, vesionbean.class);

                                    if (v.isSuccess()) {
                                        szUrl = v.getData().getUrl();
                                        String szInfo = "";
                                        String szSplit = "";
                                        for(String iTem : v.getData().getLog()){
                                            szInfo += szSplit + iTem;
                                            szSplit = "\n";
                                        }

                                        CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                                        builder.setMessage(szInfo);
                                        builder.setTitle("发现新版本[" + v.getData().getVersion() + "]");
                                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                AutoUpGrade ver = new AutoUpGrade();
                                                ver.mContext = mContext;
                                                ver.downloadAPK(szUrl, "ams.apk");
                                                dialog.dismiss();
                                            }
                                        });

                                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                dialogInterface.dismiss();
                                            }
                                        });

                                        builder.create().show();
                                    }
                                }
                            }

                            @Override
                            public void OnRuningState(int nStatus) {
                                switch (nStatus) {
                                    case SystemEnvironmental.StartGetDataFromServer:
                                        //tvVersion.setText("版本：正在检测新版本...");
                                        break;
                                    case SystemEnvironmental.StopGetDataFromServer:
                                        //tvVersion.setText("版本：" + SystemApplication.SystemConfig.getAppConfig().getVersion());
                                        break;
                                }
                            }

                            @Override
                            public String OnGetCustomParam() {
                                return null;
                            }
                        });


            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
